package com.jy.horny.function.me

import android.net.Uri
import com.jy.chat.kit.RongIM
import com.jy.horny.base.BaseVMFragment
import com.jy.horny.base.HttpRequestListener
import com.jy.horny.databinding.FragmentMeBinding
import com.jy.horny.dialog.HelpCenterPopup
import com.jy.horny.function.editinfo.EditProfileActivity
import com.jy.horny.function.me.viewmodel.MeViewModel
import com.jy.horny.function.settings.SettingsActivity
import com.jy.horny.model.UserInfoModel
import com.jy.horny.utils.loadThumbnails
import com.lib.module_base.utils.SaveDeviceUtil
import io.rong.imlib.model.UserInfo

class MeFragment:BaseVMFragment<FragmentMeBinding, MeViewModel>(FragmentMeBinding::inflate,
    MeViewModel::class.java) {
    override fun initListener() {
        super.initListener()
        binding?.helpCenterContainer?.setOnClickListener{
            activity?.let { it1 -> HelpCenterPopup(it1).showPopupWindow() }
        }
        binding?.settingsContainer?.setOnClickListener {
            SettingsActivity.start(activity)
        }
        binding?.editProfileContainer?.setOnClickListener {
            if(activity==null) return@setOnClickListener
            EditProfileActivity.start(activity)
        }
    }
    override fun initData() {
        mViewModel?.getUserInfo(object :HttpRequestListener<UserInfoModel>(){
            override fun addRequestParams(params: HashMap<String, Any>) {

            }

            override fun success(t: UserInfoModel?) {
                t?.apply {
                    binding?.imgMeAvatar?.width?.let {
                        binding?.imgMeAvatar?.loadThumbnails(t.avatarUrl,
                            it
                        )
                    }
                    binding?.txtName?.text="${t.nickName} · ${t.age}"
                    SaveDeviceUtil.saveUserCode(t.userCode)
                    RongIM.getInstance().refreshUserInfoCache(
                        UserInfo(
                            t.userCode,
                            t.nickName,
                            Uri.parse(t.avatarUrl)
                        )
                    )
                }
            }

        })
    }
}